function Promise(executor) {
  let self = this
  // 保存成功的值和失败的原因
  self.value = undefined
  self.reason = undefined
  // 专门保存成功的回调
  self.onResolvedCallbacks = []
  // 专门保存失败的回调
  self.onRejectedCallbacks = []
  // 保存当前这个promise的状态(3个状态pending、resolved，rejected)
  self.status = 'pending'
  function resolve(value) {
    if(self.status === 'pending') {
      self.value = value
      self.status = 'resolved'
      self.onResolvedCallbacks.forEach(fn => {
        fn()
      });
    }
  }
  function reject(reason) {
    if(self.status === 'pending') {
      self.reason = reason
      self.status = 'rejected'
      self.onRejectedCallbacks.forEach(fn => {
        fn()
      });
    }
  }
  // executor是立即执行的
  try {
    executor(resolve, reject)
  } catch (error) {
    reject(error)
  }
}
// 解析链式调用（他还要和其他的promise进行结合）
function resolvePromise(x, promise2, resolve, reject){
  // console.log('x', x)
  // console.log('promise2', promise2)
  // console.log(x === promise2)
  if(x === promise2) { // 自己不能等待自己
    return reject(new TypeError('循环引用'))
  }
  // 如果x是一个函数 或者x是一个对象  就有可能x是一个promise
  if(x !== null && (typeof x === 'function' || typeof x === 'object')) {
    try {
      let then = x.then;
      if(typeof then == 'function') { // 是promise
        then.call(x, function(y){
          resolve(y)
        }, function(error){
          reject(error)
        })
      } else {
        resolve(x)
      }
    } catch (error) { // 如果x取then的时候可能会发生异常， 如果有异常
      reject(error)
    }
  } else{ // 普通值的情况直接成功即可
    resolve(x)
  }

}
// then方法的实现
Promise.prototype.then = function(onFulfilled, onRejected) {
  let self = this
  // 调用then后返回一个promise
  let promise2 = new Promise(function(resolve, reject){
    if(self.status === 'resolved') {
      // 我们限制需要做的事情就是把then中成功活失败后函数执行的结果获取到
      // 看看是不是promise 如果是promise 就让promise执行 取到最终这个promise的执行结果
      // ，让返回的promise 成功或者失败
      // 如果x是普通值就让这个返回promise变成成功态
      let x = onFulfilled(self.value)
      resolvePromise(x, promise2, resolve, reject)
    }
    if(self.status === 'rejected') {
      let x = onRejected(self.reason)
      resolvePromise(x, promise2, resolve, reject)
    }
  // executor中有一部操作，此时调用then时，处于等待状态
    if(self.status === 'pending') {
      self.onResolvedCallbacks.push(function(){
        let x = onFulfilled(self.value)
        resolvePromise(x, promise2, resolve, reject)
      })
    }
    if(self.status === 'pending') {
      self.onRejectedCallbacks.push(function(){
        let x = onRejected(self.reason)
        resolvePromise(x, promise2, resolve, reject)
      })
    }
  })

  return promise2

}

module.exports = Promise